package io.yizoo.system.controller;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.yizoo.business.entity.biz_hrm_ei_basic;
import io.yizoo.business.entity.biz_hrm_ei_rel_org;
import io.elm.core.CoreController;
import io.yizoo.system.entity.sys_file;
import io.yizoo.system.entity.sys_user;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.EnumSet;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/system/user")
public class userController extends CoreController {

    private static final PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
    @Override
    protected void Init() {
        PowerEntity = sys_user.class;
        PowerUse = EnumSet.of(Power.IDDelete, Power.IDAny, Power.IDGetSingle,Power.IDUpsert);
    }
    @PostMapping("/GetData")
    public Object GetData(@RequestBody String jsonStr) {
        List<Map<String,Object>> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .Select("uu.*,ei.id AS ei_id,ei.name AS ei_name,ei.sex AS ei_sex,ei.phone AS ei_phone,rr.org_id AS ei_org_id")
                        .From(sys_user.class,"uu")
                        .LeftJoin(biz_hrm_ei_basic.class,"ei","ei.emp_code = uu.emp_code")
                        .LeftJoin(biz_hrm_ei_rel_org.class,"rr","rr.ei_id = ei.id AND rr.pm='1' ")
                        .IsWhere("and uu.nick_name like CONCAT('%', #{name}, '%')","name")
                        .IsWhere("and uu.dept_id=#{org}","org")
                        .IsPage()
                        .GetFactor());

        //若有人员关联，那么几个字段值用人员表记录的对应字段值
        for(Map<String,Object> item :ret){
            if(item.get("ei_id")!=null){
                if(item.get("ei_name")!=null){item.put("nick_name",item.get("ei_name").toString());}
                if(item.get("ei_phone")!=null){item.put("phonenumber",item.get("ei_phone").toString());}
                if(item.get("ei_sex")!=null){item.put("sex",item.get("ei_sex").toString());}
                if(item.get("ei_org_id")!=null){item.put("dept_id",item.get("ei_org_id").toString());}
            }
        }

        return JsonResultPage(ret);
    }

    @PostMapping("/Any")
    public Object Any(@RequestBody String jsonStr) {
        sys_user mod = Ctor.HMapper.GetSingle(sys_user.class,
                Ctor.DMaker(jsonStr).Selector
                        .From(sys_user.class)
                        .IsWhere("and user_name = #{user_name}","user_name")
                        .IsWhere("and id <> #{id}","id")
                        .Limit("limit 1")
                        .GetFactor());
        return JsonResult(mod!=null);
    }
    @PostMapping("/Upsert")
    public Object Upsert(@RequestBody String jsonStr) {
        ObjectNode obj = Ctor.getObjectNode(jsonStr);
        obj.put("password",passwordEncoder.encode(obj.get("password").asText()));
        Object ret = Ctor.HMapper.Upsert(sys_user.class,obj,null);
        return JsonResult(ret != null ? 1 : 0);
    }

    @PostMapping("/ReSetPassword")
    public Object ReSetPassword(@RequestBody String jsonStr) {
        ObjectNode node = Ctor.getObjectNode(jsonStr);
        node.put("init_password", 1);
        node.put("password", passwordEncoder.encode(node.get("password").asText()));
        Object ret = Ctor.HMapper.Upsert(sys_user.class,node,null);
        return JsonResult(ret!=null?1:0);
    }
    @PostMapping("/GetSingle")
    public Object GetSingle(@RequestBody String jsonStr) {
        Object ret = Ctor.HMapper.GetSingle(sys_user.class,
                Ctor.DMaker(jsonStr).Selector
                        .From(sys_user.class)
                        .IsWhere("and user_name = #{user_name}","user_name")
                        .OrderBy("id desc")
                        .Limit("limit 1")
                        .GetFactor());
        return JsonResult(ret);
    }

}
